import { Component, ViewChild, Injector, Output, EventEmitter, ElementRef } from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';
import { ComboboxItemDto, PipelineNodesServiceProxy, OrganizationUnitServiceProxy, GetNodeWorkOutput, NameValueDto, SetNodeWorkersInput, NodeWorkerDto} from '@shared/service-proxies/service-proxies';
import { AppComponentBase } from '@shared/common/app-component-base';

import * as moment from 'moment';
import { finalize } from 'rxjs/operators';

@Component({
  selector: 'createOrEditTaskworkingModal',
  styleUrls: ['./create-or-edit-taskworking-modal.component.less'],
  templateUrl: './create-or-edit-taskworking-modal.component.html'
})

export class CreateOrEditTaskworkingModalComponent extends AppComponentBase {

  @ViewChild('createOrEditModal') modal: ModalDirective;
  @ViewChild('dueDatePicker') dueDatePicker: ElementRef;

  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
  id: number;
  active = false;
  saving = false;
  pipes: ComboboxItemDto[] = [];
  pipe: ComboboxItemDto;
  nodes: ComboboxItemDto[] = [];
  node: ComboboxItemDto;
  nodeWorker = new Array<GetNodeWorkOutput>();
  newCar: boolean;
  displayDialog: boolean;
  setNodeWorkers = new SetNodeWorkersInput();
  users = new Array<NameValueDto>();
  constructor(
    injector: Injector,
    private _taskServiceProxy: PipelineNodesServiceProxy,
    private _organizationUnitServiceProxy: OrganizationUnitServiceProxy
  ) {
    super(injector);
  }

  show(): void {
    this._taskServiceProxy.getPipelineList().subscribe(result => {
        this.pipes = result;
        console.log(this.pipes);
        if ( this.pipes !== null) {
            this.pipe = result[0];
            this.changepipe();
        }
      });
    this.active = true;
    this.modal.show();
  }
  changepipe(){
    //console.log(this.pipe);
    this.users=null;
    this._taskServiceProxy.getPipelineNodeList(this.pipe.value).subscribe(result => {
        this.nodes = result;
        if ( this.nodes !== null) {
            this.node = result[0];
            this.showTaskWork();
        }
      });
  }
  showTaskWork(){
    this.primengTableHelper.showLoadingIndicator();
    this.users=null;
    this._taskServiceProxy.getNodeWorkers(this.node.value).subscribe(result => {
        this.nodeWorker = result;
        this._organizationUnitServiceProxy
        .findUsersForNode()
        .subscribe(result => {
            this.users = result;
            this.primengTableHelper.hideLoadingIndicator();
        });
    });
  }
  showDialogToAdd() {
    let selected = new GetNodeWorkOutput();
    selected.userName = new NameValueDto();
    selected.userName = this.users[0];
    selected.timePercentage = 100;
    this.nodeWorker.push(selected);

}
  save(): void {
    this.saving = true;
    let nodeWorkers = new Array<NodeWorkerDto>();
    let sum = 0;
    this.nodeWorker.forEach(item => {
        let nodeWorker = new NodeWorkerDto();
        nodeWorker.userId = Number(item.userName.value);
        nodeWorker.userName = item.userName.name;
        nodeWorker.timePercentage = item.timePercentage;
        nodeWorker.id = item.id;
        nodeWorker.nodeEntityId = this.node.value;
        nodeWorker.pipelineEntityId = this.pipe.value;
        sum = sum + item.timePercentage;
        nodeWorkers.push(nodeWorker);
    });
    if ( sum !== 100 ) {
        this.notify.error('总工时不为100.请检查');
        this.saving = false;
    } else {
        this.setNodeWorkers.workers = nodeWorkers;
        this.setNodeWorkers.nodeId = this.node.value;
        this.setNodeWorkers.pipelineId = this.pipe.value;
        console.log(this.setNodeWorkers);
        this._taskServiceProxy
            .setNodeWorkers(this.setNodeWorkers)
            .subscribe(result => { this.notify.success('保存成功'); });
            this.modalSave.emit(null);
             this.close();
        }
  }
  close(): void {
    this.active = false;
    this.modal.hide();
  }
  onRowEditInit(data){
    console.log(data);
    console.log(data);
    let nodeWorkers = new Array<GetNodeWorkOutput>();
    for (let index = 0; index < this.nodeWorker.length; index++) {
        //let nodeWorker = new GetNodeWorkOutput();
        if ( index !== data) {
            nodeWorkers.push(this.nodeWorker[index]);
        }

    }
    this.nodeWorker = nodeWorkers;
    console.log(this.nodeWorker);
    //this.nodeWorker(selected);
  }
}
